#pragma once
#include "RBTreeGeneric.h"

namespace cxf
{
	template<class K, class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const std::pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, std::pair<const K, V>, MapKeyOfT>::Iterator iterator;
		typedef typename RBTree<K, std::pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;
		iterator begin()
		{
			return _root.Begin();
		}
		iterator end()
		{
			return _root.End();
		}
		const_iterator begin() const
		{
			return _root.Begin();
		}
		const_iterator end() const
		{
			return _root.End();
		}
		std::pair<iterator, bool> insert(const std::pair<K, V>& kv)
		{
			return _root.Insert(kv);
		}
		bool erase(const K& key)
		{
			return _root.Erase(key);
		}
		iterator find(const K& key)
		{
			return _root.Find(key);
		}
		V& operator[](const K& key)
		{
			std::pair<iterator, bool> ret = insert(std::make_pair(key, V()));
			return ret.first->second;
		}
	private:
		RBTree<K, std::pair<const K, V>, MapKeyOfT> _root;
	};
}